package com.dbh.jdk.concurrent;


import java.util.concurrent.locks.LockSupport;

/**
 *  线程阻塞工具类示例
 *      LockSupport的静态方法park()可以阻塞当前线程，类似的parkNanos()、parkUntil()等方法。
 *      park()方法不会导致线程永久性挂起
 *
 * @author dongbanghui
 * @date 2018/06/02
 */
public class LockSupportDemo {

    public static Object u = new Object();

    static ChangeObjectThread t1 = new ChangeObjectThread("t1");
    static ChangeObjectThread t2 = new ChangeObjectThread("t2");

    public static class ChangeObjectThread extends Thread{
        public ChangeObjectThread(String name){
            super.setName(name);
        }

        @Override
        public void run() {
            synchronized (u){
                System.out.println("in " + getName());
                LockSupport.park();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        t1.start();
        Thread.sleep(100);
        t2.start();
        LockSupport.unpark(t1);
        LockSupport.unpark(t2);
        t1.join();
        t2.join();
    }
}
